﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using Noris.Tools.TraceVisualiser.Support;

namespace Noris.Tools.TraceVisualiser.Components
{
    /// <summary>
    /// Panel pro snadné vybírání barvy (GetColor)
    /// </summary>
	[DefaultEvent("SelectedColorChanged")]
    internal class ColorSelectPanel : DPanel
	{
		#region Vnitřní konstrukce
        public ColorSelectPanel()
		{
			this._Init();
		}
		private Panel Col;
		private Button Btn;
		private void _Init()
		{
			this.Col = new Panel();
            this.Col.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.Col.Location = new Point(0, 0);
			this.Col.Size = new Size(253, 22);

			this.Btn = new Button();
			this.Btn.Location = new Point(255, 0);
			this.Btn.Size = new Size(25, 22);
			this.Btn.Text = "...";
			this.Btn.Click += new EventHandler(Btn_Click);

			this.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.Controls.Add(this.Col);
			this.Controls.Add(this.Btn);
			this.Size = new Size(280, 22);
			this.MinimumSize = new Size(75, 22);
			this.MaximumSize = new Size(3600, 22);

			this.Col.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top);
			this.Btn.Anchor = (AnchorStyles)(AnchorStyles.Right | AnchorStyles.Top);

		}
		void Btn_Click(object sender, EventArgs e)
		{
            this._OpenDialog();
        }
		#endregion
        #region Property a eventy
        /// <summary>
        /// Aktuálně zadaná barva
        /// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(true)]
		public Color SelectedColor
		{
			get { return this._SelectedColor; }
			set { this._SetSelectedColor(value); }
		}
		/// <summary>
		/// Nápovědný titulek v dialogovém okně
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Category("Appearance")]
		[Description("Nápovědný titulek v dialogovém okně")]
		public string Title { get; set; }
		/// <summary>
        /// Event volaný po změně hodnoty SelectedColor.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Category("Action")]
        [Description("Event volaný po změně hodnoty SelectedColor.")]
		public event EventHandler SelectedColorChanged;
        #endregion
        #region Výkonné metody
        private void _OpenDialog()
        {
            using (ColorDialog cd = new ColorDialog())
            {
                cd.AllowFullOpen = true;
                cd.AnyColor = true;
                cd.Color = this.SelectedColor;
                cd.CustomColors = Steward.AppConfig.CustomColors;
                cd.FullOpen = false;
                cd.SolidColorOnly = false;
                var result = cd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    Steward.AppConfig.CustomColors = cd.CustomColors;
                    this.SelectedColor = cd.Color;
                }
            }
        }
		private void OnSelectedColorChanged()
		{
			if (this.SelectedColorChanged != null)
				this.SelectedColorChanged(this, EventArgs.Empty);
		}
		/// <summary>
		/// Privátní property, setování detekuje změnu a při změně volá event SelectedPathChanged.
		/// </summary>
        private Color _SelectedColor
		{
			get { return __SelectedColor;}
			set
			{
				bool isChange = (value != __SelectedColor);
				__SelectedColor = value;
				if (isChange)
					OnSelectedColorChanged();
			}
		}
		private Color __SelectedColor;
		private void _SetSelectedColor(Color color)
		{
            this.Col.BackColor = color;
			this._SelectedColor = color;
		}
        #endregion
    }
}
